package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.LiveHomeBean;
import com.squareup.picasso.Picasso;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XINDZ on 2017/2/14.
 * 功能:直播首页的Adapter
 */

public class LiveHomeAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    List<LiveHomeBean.ListBean> liveHomeBeanList = new ArrayList();
    Context context;
    FinalBitmap fb;


    public LiveHomeAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        fb = FinalBitmap.create(context);
    }

    public LiveHomeAdapter(Context context, List<LiveHomeBean.ListBean> liveHomeBeanList) {
        this.context = context;
        this.liveHomeBeanList = liveHomeBeanList;
        mInflater = LayoutInflater.from(context);
        fb = FinalBitmap.create(context);
    }


    public void setData(List<LiveHomeBean.ListBean> liveHomeBeanList) {
        this.liveHomeBeanList.clear();
        this.liveHomeBeanList = liveHomeBeanList;
        notifyDataSetChanged();
    }

    public void addData(List<LiveHomeBean.ListBean> liveHomeBeanList) {
        this.liveHomeBeanList = liveHomeBeanList;
        notifyDataSetChanged();
    }

    public void clearData() {
        if (liveHomeBeanList != null) {
            this.liveHomeBeanList.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return liveHomeBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        if (i < 0) {
            i = 0;
        } else if (i > liveHomeBeanList.size()) {
            i = liveHomeBeanList.size();
        }
        return liveHomeBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LiveViewHolder liveViewHolder;
        if (view == null) {
            liveViewHolder = new LiveViewHolder();
            view = mInflater.inflate(R.layout.item_livehome, null);
            liveViewHolder.tvLiveState = (TextView) view.findViewById(R.id.tv_item_livehome_state);
            liveViewHolder.ivLiveBg = (ImageView) view.findViewById(R.id.iv_item_livehome_bg);
            liveViewHolder.tvLiveName = (TextView) view.findViewById(R.id.tv_item_livehome_name);
            liveViewHolder.tvLiveLookNum = (TextView) view.findViewById(R.id.tv_item_livehome_looknum);
            liveViewHolder.rlLiveHome = (RelativeLayout) view.findViewById(R.id.rl_item_livehome);
            view.setTag(liveViewHolder);
        } else {
            liveViewHolder = (LiveViewHolder) view.getTag();
        }
        LiveHomeBean.ListBean listBean = liveHomeBeanList.get(i);
        String img = listBean.getImg();
        String time = listBean.getStime();
        String title = listBean.getTitle();
        int state = listBean.getState();
        switch (state) {
            case 1://正在直播
                liveViewHolder.tvLiveState.setText("直播中");
                break;
            case 2://未开始直播
                liveViewHolder.tvLiveState.setText("未开始");
                break;
            case 3://直播结束
                liveViewHolder.tvLiveState.setText("已结束");
                break;
            default:
                break;
        }
        liveViewHolder.tvLiveName.setText(title);
        liveViewHolder.tvLiveLookNum.setText(time);
        if (img != null && !img.equals("")) {
            Picasso.with(context).load(img).placeholder(R.drawable.default21).error(R.drawable.default21).into(liveViewHolder.ivLiveBg);
        } else {
            Picasso.with(context).load(R.drawable.default21).into(liveViewHolder.ivLiveBg);
        }
        return view;
    }

    static class LiveViewHolder {
        ImageView ivLiveBg;
        TextView tvLiveName, tvLiveLookNum, tvLiveState;
        RelativeLayout rlLiveHome;

    }
}
